package org.tigr.htc.server.sge;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/**
 * The <b>SGEJobStatus</b> class is used to query the SGEjob
 * status.
 */
public class SgeJobStatus {
	private boolean idle = true;
    private boolean running = false;
    private boolean complete = false;
    private boolean successful = false;
    private boolean held = false;
    private int returnValue = 0;
    private String host;
    private String statusLine = null;
    
	/**
	 * The method <code>isComplete</code> returns the value for complete
	 * 
	 * @return Returns a <code>boolean</code> value for the field complete.
	 */
	public boolean isComplete() {
		return complete;
	}

	/** 
	 * The method <code>setComplete</code> sets the value for complete
	 * 
	 * @param complete A <code>boolean</code> value for the field complete.
	 */
	public void setComplete(boolean complete) {
		this.complete = complete;
	}

	/**
	 * The method <code>getHost</code> returns the value for host on which the job is running. It 
     * typically includes the host name and the specific queue on the host.
	 * 
	 * @return Returns a <code>String</code> value for the field host.
	 */
	public String getHost() {
		return host;
	}

	/** 
	 * The method <code>setHost</code> sets the value for host
	 * 
	 * @param host A <code>String</code> value for the field host.
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * The method <code>isIdle</code> returns the value for idle
	 * 
	 * @return Returns a <code>boolean</code> value of true is the job is idling, false otherwise
	 */
	public boolean isIdle() {
		return idle;
	}

	/** 
	 * The method <code>setIdle</code> sets the value for idle
	 * 
	 * @param idle A <code>boolean</code> value for the field idle.
	 */
	public void setIdle(boolean idle) {
		this.idle = idle;
	}

	/**
	 * The method <code>getReturnValue</code> returns the value for returnValue
	 * 
	 * @return Returns a <code>int</code> value for the field returnValue.
	 */
	public int getReturnValue() {
		return returnValue;
	}

	/** 
	 * The method <code>setReturnValue</code> sets the value for returnValue
	 * 
	 * @param returnValue A <code>int</code> value for the field returnValue.
	 */
	public void setReturnValue(int returnValue) {
		this.returnValue = returnValue;
	}

	/**
	 * The method <code>isRunning</code> returns the value for running
	 * 
	 * @return Returns a <code>boolean</code> value of true if the job is in the running state, false
     * otherwise.
	 */
	public boolean isRunning() {
		return running;
	}

	/** 
	 * The method <code>setRunning</code> sets the running state for the job status.
	 * 
	 * @param running A <code>boolean</code> value for the field running.
	 */
	public void setRunning(boolean running) {
		this.running = running;
        this.idle = false; // As it is running, set idle false
	}

	/**
	 * The method <code>isSuccessful</code> returns the successful status of the job.
	 * 
	 * @return Returns a <code>boolean</code> value of true of the job was successful, false otherwise
	 */
	public boolean isSuccessful() {
		return successful;
	}

	/** 
	 * The method <code>setSuccessful</code> sets the successful state of the job
	 * 
	 * @param successful A <code>boolean</code> value for the field successful.
	 */
	public void setSuccessful(boolean successful) {
		this.successful = successful;
	}

    /**
     * The method <code>reset</code> resets the job status to the original state. This 
     * method is typically used by the monitor to reset the state rather than creating a 
     * new object
     * 
     * 
     */
    public void reset() {
		idle = true;
		running = false;
		complete = false;
		successful = false;
		held = false;
		returnValue = 0;
		host = "";
	}

    public String toString () {
    	StringBuffer sbuf = new StringBuffer();
        String state = null;

        if (idle) state = "idle";
        if (complete) state = "complete";
        if (running) {
            state = "running";
        }
        
        if (held) {
        	state = "he:q" +
        			"ld";
        }
        sbuf.append("State: " + state);
        
        if (running) {
        	sbuf.append("Host: " + host);
        }
        
        if (complete) {
			sbuf.append("Successful: ").append(successful);
            sbuf.append("Return value: ").append(
				returnValue);
		}
        return sbuf.toString();
    }
    
	/**
	 * The method <code>isHeld</code> returns held status of the job
	 * 
	 * @return Returns a <code>boolean</code> value of true if the job has ben held, false otherwise.
	 */
	public boolean isHeld() {
		return held;
	}

	/** 
	 * The method <code>setHeld</code> sets the held status.
	 * 
	 * @param held A <code>boolean</code> value for the field held.
	 */
	public void setHeld(boolean held) {
		this.held = held;
	}

	/**
	 * The method <code>getstatusLine</code> returns the value for statusLine that was read when the
     * status was read last.
	 * 
	 * @return Returns a <code>String</code> value for the field statusLine.
	 */
	public String getStatusLine() {
		return statusLine;
	}

	/** 
	 * The method <code>setstatusLine</code> sets the value for statusLine
	 * 
	 * @param statusLine A <code>String</code> value for the field statusLine.
	 */
	public void setStatusLine(String statusLine) {
		this.statusLine = statusLine;
	}
    
    
    /**
     * The method <code>isStatusLineChanged</code> is used to see if the status has 
     * changed. This is accomplished by comparing the previous status line to the new
     * one.
     * 
     * @param statusLine a <code>String</code> representing the new status line
     * 
     * @return a <code>boolean</code> value of true if the status has changed, false otherwise
     */
    public boolean isStatusLineChanged(String statusLine) {
        // If the previous status line is empty then return that it has
        // changed as the new line is bound to be non-null
        if (this.statusLine == null) {
        	return true;
        }
        
    	return ! this.statusLine.equals(statusLine);
    }

}
